package cn.iocoder.yudao.module.promotion.groupon.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.controller.DetailController;
import cn.iocoder.yudao.framework.business.basic.controller.ListController;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.promotion.gift.dto.GiftActivityVO;
import cn.iocoder.yudao.module.promotion.gift.service.core.CoreGiftRecordService;
import cn.iocoder.yudao.module.promotion.groupon.pojo.dto.AppGrouponPackageQueryDTO;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.AppGrouponPackageVO;
import cn.iocoder.yudao.module.promotion.groupon.service.app.AppGrouponPackageFavoriteService;
import cn.iocoder.yudao.module.promotion.groupon.service.app.AppGrouponPackageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 团购套餐")
@RestController
@RequestMapping("/promotion/groupon-package")
@Validated
public class AppGrouponPackageController implements DetailController<AppGrouponPackageVO, Long>, ListController<AppGrouponPackageVO, AppGrouponPackageQueryDTO>, PageController<AppGrouponPackageVO, AppGrouponPackageQueryDTO> {

    @Resource
    private AppGrouponPackageService appGrouponPackageService;
    @Resource
    private AppGrouponPackageFavoriteService appGrouponPackageFavoriteService;
    @Resource
    private CoreGiftRecordService coreGiftRecordService;

    @PermitAll
    @Override
    public CommonResult<AppGrouponPackageVO> getDetail(Long id) {
        AppGrouponPackageVO data = appGrouponPackageService.getDetail(id);
        data.setFavorite(appGrouponPackageFavoriteService.isTrue(id, getLoginUserId()));
        return success(data);
    }

    @PermitAll
    @Override
    public CommonResult<List<AppGrouponPackageVO>> getList(AppGrouponPackageQueryDTO param) {
        Assertions.notNull(param.getShopId(), "未获取到 shopId");
        List<AppGrouponPackageVO> data = appGrouponPackageService.getList(param);
        Set<Long> giftActivityIds = new HashSet<>();
        for (AppGrouponPackageVO item : data) {
            if (CollUtil.isNotEmpty(item.getGiftActivityIds())) {
                giftActivityIds.addAll(item.getGiftActivityIds());
            }
        }
        if (CollUtil.isNotEmpty(giftActivityIds)) {
            Map<Long, Integer> countMap = coreGiftRecordService.getTakenCount(getLoginUserId(), giftActivityIds);
            for (AppGrouponPackageVO item : data) {
                if (CollUtil.isEmpty(item.getGiftActivities())) {
                    continue;
                }
                for (GiftActivityVO vo : item.getGiftActivities()) {
                    vo.setTakeReceivedCount(countMap.getOrDefault(vo.getId(), 0));
                }
            }
        }
        wrapFavorite(data);
        return success(data);
    }

    @PermitAll
    @Override
    public CommonResult<PageResult<AppGrouponPackageVO>> getPage(Integer pageNo, Integer pageSize, AppGrouponPackageQueryDTO param) {
        PageResult<AppGrouponPackageVO> page = appGrouponPackageService.getPage(pageNo, pageSize, param);
        List<AppGrouponPackageVO> list = page.getList();
        wrapFavorite(list);
        return success(page);
    }

    private void wrapFavorite(List<AppGrouponPackageVO> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        Set<Long> bizIds = list.stream().map(AppGrouponPackageVO::getId).collect(Collectors.toSet());
        Map<Long, Boolean> stateMap = appGrouponPackageFavoriteService.getStateMap(bizIds, getLoginUserId());
        list.forEach(item -> item.setFavorite(Boolean.TRUE.equals(stateMap.get(item.getId()))));
    }

}
